<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<!--父组件-->
<div id="app">
  <keep-alive>
    <component :is="cItem"></component>
  </keep-alive>
  <button @click="change">切换</button>
</div>

<!--子组件1模板-->
<template id="box1">
  <input type="text">
</template>
<!--子组件2模板-->
<template id="box2">
  <button>按钮</button>
</template>



<script src="../js/vue.js"></script>
<script>

  // 注册子组件1
  const LKInput = {
    template: "#box1"
  }
  // 注册子组件2
  const LKButton = {
    template: "#box2"
  }

  // 父组件
  const app = Vue.createApp({
    data(){
      return {
        cItem: 'lk-input'
      }
    },
    methods:{
      change(){
        if(this.cItem === 'lk-button'){
          this.cItem = 'lk-input'
        }else{
          this.cItem = 'lk-button'
        }
      },
    },
    components: {
      // 挂载子组件1和2
      'lk-input': LKInput,
      'lk-button': LKButton,
    },
  });
  // 挂载vue实例
  app.mount('#app');
</script>
</body>
</html>